package drds.server.sqlparser.sqlobject.statement.table;

import drds.server.sqlparser.sqlobject.expression.Expression;
import drds.server.sqlparser.visitor.Visitor;

/**
 * 
 * @author 曹正辉<330937205@qq.com>
 * 
 */
public class ExpressionTable extends AbstractTable {

	protected Expression expression;

	public ExpressionTable() {

	}

	public ExpressionTable(Expression expression) {
		this(expression, null);
	}

	public ExpressionTable(Expression expression, String alias) {
		this.setExpression(expression);
		this.setAlias(alias);
	}

	public void setExpression(Expression expression) {
		if (expression != null) {
			expression.setParent(this);
		}
		this.expression = expression;
	}

	public Expression getExpression() {
		return this.expression;
	}

	@Override
	protected void accept0(Visitor visitor) {
		if (visitor.visit(this)) {
			acceptChild(visitor, this.expression);
		}
		visitor.endVisit(this);
	}

	public void output(StringBuffer sb) {
		this.expression.output(sb);
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((expression == null) ? 0 : expression.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object o) {
		if (this == o)
			return true;
		if (o == null)
			return false;
		if (getClass() != o.getClass())
			return false;
		ExpressionTable other = (ExpressionTable) o;
		if (expression == null) {
			if (other.expression != null)
				return false;
		} else if (!expression.equals(other.expression))
			return false;
		return true;
	}

}
